<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ include file="/common/taglibs.jsp" %>
<html>
<head>
  <%@ include file="/common/meta.jsp" %>
  <title>多任务节点</title>
  <script type="text/javascript">
    var childNodeStr = sessionStorage.getItem("childNode");
    var edit = "add";//用于表示新增/修改
    var rowNo;//列表的行号
    $(function(){



      $("input[type=button]").css({"background":"#ffffff","display":"inline-block","cursor":"pointer"});
      /*绑定查看页面下拉框*/
      $("#viewPageName").ligerComboBox({
        url:'${_ctx}/flow/workPage/getComboBoxData?processId=${processId}',
        valueField: 'id',
        textField: 'pageName',
        valueFieldID: 'viewPageId',
        selectBoxWidth: 170,
        width: 170,
        highLight:true,
        cancelable:true
      });


      $("#mainGrid").ligerGrid({
        columns: [
          { display: 'id', name: 'id', hide: true, width: 60 },
          { display: 'id', name: 'childNodeId', hide: true, width: 60 },
          { display: '序号',name:'no', width: 40, render: function (rowData, rowindex, value, column, rowid, page, pagesize) { return (page - 1) * pagesize + rowindex + 1; } },
          { display: '工作任务', name: 'workTasks', align: 'center', width: 180 },
          { display: '下步节点', name: 'nextNode', align: 'center', width: 160 }
        ],
        width: '100%', heightDiff: -1,
        usePager:false
      });
      /*接收并初始化节点*/
      $("#currentNodeName").val(nodeObj['${currentNodeId}'].name);
      $("#navtab1").ligerTab();
      toolbar();
      /*调用加载多任务节点的属性信息方法*/
      loadMultiTask();

    });


    /*加载多任务节点的属性信息*/
    function loadMultiTask(){
      var properties = JSON.parse(sessionStorage.getItem('properties'));
      if(properties == null){
        return;
      }
      //查看页面
      if(properties['${currentNodeId}']){
        var viewPageId = properties['${currentNodeId}'].viewPageId;
        if(viewPageId){
          $.ajax({
            url:'${_ctx}/flow/workPage/getPageNameById',
            type:'post',
            dataType:'json',
            data:{viewPageId:viewPageId},
            success:function(page){
              $("#viewPageName").ligerComboBox().setText(page.viewPageName);
              $("#viewPageId").val(viewPageId);
            }
          });
        }
        //复选框
        var allowStop = properties['${currentNodeId}'].allowStop;
        var allowRecall = properties['${currentNodeId}'].allowRecall;
        var allowHang = properties['${currentNodeId}'].allowHang;
        if(allowStop == 1){
          $("#allowStop").attr("checked",true);
        }
        if(allowRecall == 1){
          $("#allowRecall").attr("checked",true);
        }
        if(allowHang == 1){
          $("#allowHang").attr("checked",true);
        }
      }



      //子任务列表
      if(childNodeStr!=undefined && childNodeStr!=""){
        allChildTasks = JSON.parse(childNodeStr);
      }
      for(var key in allChildTasks){
        var nextNodeValue = allChildTasks[key].nextNodeId;
        var workName = allChildTasks[key].name;
        var nextNodeName = '';
        if(nodeObj[nextNodeValue]){
          nextNodeName = nodeObj[nextNodeValue].name;
        }
        var manager = $("#mainGrid").ligerGetGridManager();
        var firstRow = manager.getRow(0);
        manager.addRow({
          id:key,
          childNodeId:nextNodeValue,
          workTasks:workName,
          nextNode:nextNodeName
        }, firstRow, true);
      }
    }
    /*子任务弹窗*/
    var allChildTasks={};
    function addChildTask(id){
      var mydate = new Date();
      sessionStorage.setItem("allChildTasks",JSON.stringify(allChildTasks));
      parent.$.ligerDialog.open({title: "新增/修改子任务", width:730,height:550, allowClose: false, url: '${_ctx}/flow/process/childTask?processId=${processId}&childNodeId='+id,
        buttons: [
          { text: '确定', onclick: function (item, dialog) {
            var nodeJson = dialog.frame.createJson();
            if(dialog.frame.fkId() == ""){
              parent.BAP.showWarn("流程执行人不能为空");
              return;
            }
            if( dialog.frame.isNoticesNull){
              var nextNodeName = dialog.frame.$("#node").find("option:selected").text();
              var nextNodeValue = dialog.frame.$("#node").val();
              var manager = $("#mainGrid").ligerGetGridManager();
              conditions=[];
              conditions = dialog.frame.expressNodeId();
              var childTasksKey = '${currentNodeId}'+"-"+(mydate.getTime());
              if(edit != "update"){
                var firstRow = manager.getRow(0);
                if(nodeJson.name!="" && nextNodeName != ""){
                  allChildTasks[childTasksKey] = nodeJson;
                  manager.addRow({
                    id:childTasksKey,
                    childNodeId:nextNodeValue,
                    workTasks:nodeJson.name,
                    nextNode:nextNodeName
                  }, firstRow, true);
                }
                edit = "add";
              }else{
                allChildTasks[id] = nodeJson;
                manager.records[rowNo]['childNodeId'] = nextNodeValue;
                manager.records[rowNo]['workTasks'] = nodeJson.name;
                manager.records[rowNo]['nextNode'] = nextNodeName;
                manager.reRender();
              }
              dialog.close();
            }
          }
          },
          { text: '关闭', onclick: function (item, dialog) {
            edit = "add";
            dialog.close();
          }
          }
        ]
      });
    }

    function toolbar() {
      var items = [];
      items.push({ type: 'button', text: "增加", icon: 'add', click: function () { addChildTask(); } });
      items.push({ type: 'button', text: "删除", icon: 'delete', click: function () { deleteChildTask(); } });
      items.push({ type: 'button', text: "编辑", icon: 'delete', click: function () { editChildTask(); } });
      $("#toolbar").ligerToolBar({
        items: items
      });
      $("#mainGrid").ligerGetGridManager()._onResize();
    }

    /*获取所有子节点的Id*/
    var conditions=[];
    function getAllChildNodeId(){
      var childNodeIds=[];
      var manager = $("#mainGrid").ligerGetGridManager();
      var rows = manager.getData();
      if (rows && rows.length > 0) {
        $(rows).each(function () {
          childNodeIds.push(this.childNodeId);
        });
      }
      for(var i=0;i<conditions.length;i++){
        childNodeIds.push(conditions[i]);
      }
      return childNodeIds;
    }

    /*删除子任务*/
    function deleteChildTask(){
      var manager = $("#mainGrid").ligerGetGridManager();
      var rows = manager.getData();
      if (rows && rows.length > 0) {
        $(rows).each(function () {
          allChildTasks[this.id] = "";
        });
      }
      /*判断： 把没有值的属性删除*/
      for(var k in allChildTasks){
        if(allChildTasks[k]==undefined || allChildTasks[k] == "" || allChildTasks[k] == null || JSON.stringify(allChildTasks[k]) == "{}"){
          delete allChildTasks[k];
        }
      }
      manager.deleteSelectedRow();
    }

    /*编辑子任务*/
    function editChildTask(){
      edit = "update";
      var manager = $("#mainGrid").ligerGetGridManager();
      var rows = manager.getCheckedRows();
      var id ="";
      if (rows && rows.length > 0) {
        $(rows).each(function () {
          rowNo = this.__id;
          id = this.id;
        });
        addChildTask(id);

      }

    }

    var nodeStr = sessionStorage.getItem("nodeStr");
    var nodeObj = JSON.parse(nodeStr);

    /*创建多任务节点的json对象*/
    function createJson(){
      var allowStop = 0;
      var allowRecall = 0;
      var allowHang = 0;
      if($("#allowStop").get(0).checked){
        allowStop = 1;
      }
      if($("#allowRecall").get(0).checked){
        allowRecall = 1;
      }
      if($("#allowHang").get(0).checked){
        allowHang = 1;
      }
      var fkType="";
      //定义多任务节点json对象的模型，并赋值
      var ret = {type:nodeObj['${currentNodeId}'].type,fkId:'${currentUser}',fkType:'${userType}',viewPageId:$("#viewPageId").val(),
        allowStop:allowStop, allowRecall:allowRecall, allowHang:allowHang, tasks:allChildTasks};
      /*判断： 把没有值的属性删除*/
      for(var k in ret){
        if(ret[k]==undefined || ret[k] == "" || ret[k] == null || JSON.stringify(ret[k])=="{}"){
          delete ret[k];
        }
      }
      /*把json对象转换成json字符串*/
      return ret;
    }
  </script>
</head>
<body>
<table style="border-collapse:separate; border-spacing:10px;margin:10px auto;">
  <tr>
    <td>节点名称：</td>
    <td>
      <input type="hidden" id="currentNodeId" name="currentNodeId" style="width: 170px;" value="${currentNodeId}" />
      <input type="text" id="currentNodeName" class="liger-textbox" name="currentNodeName" style="width:170px;background:#fff;" disabled="disabled" />
    </td>
  </tr>


  <tr>
    <td>查看页面：</td>
    <td>
      <input type="hidden" id="viewPageId" name="viewPageId" />
      <input type="text" id="viewPageName" name="viewPageName" />
    </td>
  </tr>

  <tr>
    <td colspan="2">
      <input type="checkbox" id="allowStop" />允许流程发起人终止&nbsp;
      <input type="checkbox" id="allowRecall" />能否被上部撤回&nbsp;
      <input type="checkbox" id="allowHang" />允许挂起流程
    </td>
  </tr>

</table>
<div id="navtab1" style="width: 99%; overflow: hidden;margin:0 auto; border: 1px solid #D3D3d3;" class="liger-tab">
  <div tabid="home" title="任务" lselected="true" style="padding:10px;">
    <div>
      <div class="post" style="margin-top:10px;" >
        <div>
          <input type="button" value="添加" id="addChildTask" onclick="addChildTask()" style="padding:2px 10px;border:1px solid #CCCCCC;border-radius:3px;" />&nbsp;&nbsp;
          <input type="button" value="删除" id="deleteChildTask" onclick="deleteChildTask()" style="padding:2px 10px;border:1px solid #CCCCCC;border-radius:3px;" />&nbsp;&nbsp;
          <input type="button" value="编辑" id="editChildTask" onclick="editChildTask()" style="padding:2px 10px;border:1px solid #CCCCCC;border-radius:3px;" />
        </div>
        <div id="mainGrid"style="margin-top:10px;">

        </div>
      </div>

    </div>
  </div>
</div>
</body>
</html>
